﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSpace.Core;

namespace SmartSpace.Workflow.DriveEvents
{
    /// <summary>
    /// 发送给指定用户
    /// </summary>
    public class SendToUserDriveEvent : IWorkflowDriveEvent
    {
        public WorkflowDriveRecord Drive(
            WorkflowInstance instance, 
            WorkflowMetadata workflow, 
            ActivityMetadata activity, 
            EventMetadata @event, 
            WorkflowDriveRecord currentRecord, 
            Guid dataId, 
            User sender, 
            IEnumerable<Guid> receivers)
        {
            Guid receiverId = receivers.First();
            if (receiverId != Guid.Empty)
            {
                // 关闭上一步
                if (currentRecord != null)
                {
                    currentRecord.IsActivated = false;
                }

                WorkflowDriveRecord driveRecord = new WorkflowDriveRecord();
                driveRecord.Id = Guid.NewGuid();
                driveRecord.InstanceId = instance.Id;
                driveRecord.WorkflowId = instance.WorkflowId;
                driveRecord.ActivityId = @event.ToActivityId;
                driveRecord.EventId = @event.Id;
                driveRecord.SenderId = sender.Id;
                driveRecord.ReceiverId = receiverId;
                driveRecord.ReceiverType = WfReceiverType.User;
                driveRecord.DrivedDate = DateTime.Now;
                driveRecord.RecordId = dataId;
                driveRecord.IsActivated = true;
                return driveRecord;
            }
            else
            {
                return null;
            }
        }
    }
}
